<?php

class Pages extends CI_Controller {

	var $page = 'pages';
	var $action = '';


	public function index(){

			
			
		$query = $this->db->query("
					select tbl.*,
					(select CONCAT(u.firstname,'',u.middlename,' ',u.lastname) from users as u where u.id =tbl.updatedby ) as updatedby , 
					(select CONCAT(u.firstname,'',u.middlename,' ',u.lastname) from users as u where u.id =tbl.createdby ) as createdby
					from pages as tbl  where valid = 1");

		//select * from pages where valid = 1


		//echo "SELECT p.id as id, p.name AS name,p.title AS title,CONCAT(u.firstname,'',u.middlename,' ',u.lastname) AS updatedby
		//FROM pages AS p,users AS u WHERE (u.id = p.updatedby OR p.createdby = u.id and p.valid = 1";
		$data['selectedPagesInMenu'] = $this->db->query("select * from pages where menu_id != 0 and valid = 1 order by menu_id");
		$data['notSelectedPagesInMenu'] = $this->db->query("select * from pages where menu_id = 0 and valid = 1 order by menu_id");
		$this->admin($data,$query);

	}


	public function savemenuorder(){
		$menuorder = $_REQUEST['menuorder'];
		$this->db->query("update pages set menu_id = 0 ");
		//echo 'MENUUUUUUUUUUUUUUUUUUUUUUUU  '.$menuorder.length(;
		$counter = 1;
		foreach($menuorder as  $menu){
			//echo "update pages set menu_id = $counter where id = $menu,,,,,,,,,,,,,,,,,,,";
			$this->db->query("update pages set menu_id = $counter where id = $menu and valid = 1");
			$counter++;
		}
			
		$this->createLog('PAGES' , 'SAVEMENUORDER' ,  'Save Menu Order');

			
		$this->index();
			

	}


	public function add(){
		$data = '';
		$this->action = 'add';
		$sideNavList = $this->db->query("select * from pages");
		$data['sideNavList'] = $sideNavList;
			
		$data['mastheadImages'] = get_filenames("../assets/masthead/");

		$data['pagesWithNoParent']  = $this->db->query("select * from pages where (parentid ='' or parentid is NULL) and valid = 1" );
			
		$this->admin($data,null);
	}

	public function edit($id){

		$data['editId'] = $id;
		$this->action = 'edit';
			
		$data['pagesWithNoParent']  = $this->db->query("select * from pages where (parentid ='' or parentid is NULL) and valid = 1" );
			
			

		$query = $this->db->query("select * from pages where id = $id and valid  = 1 limit 1 " );
		$row = $query->row();
			
		$data['pageInfo'] = $query->row();
			
		$sideNavList = $this->db->query("select * from pages where valid = 1");
		$data['sideNavList'] = $sideNavList;
			
			
		$data['mastheadImages'] = get_filenames("../assets/masthead/");
		$this->admin($data,null);
			
	}




	public function save(){
		$createdby = $this->session->userdata('user_id');

		$name = $_REQUEST['name'];
		$content = $_REQUEST['content'];
		$title = $_REQUEST['title'];
		$metadata = $_REQUEST['metadata'];
		$pageSideNav = $_REQUEST['pageSideNav'];
		$pageMasthead = $_REQUEST['pageMasthead'];
		$parentid = $_REQUEST['parentid'];
		$isUpdate = false;
		if(isset($_REQUEST['id']))
		$isUpdate  = true;
			

		if($isUpdate){
			$query = $this->db->query("UPDATE  pages SET  name =  '$name',content =  '$content' ,title =  '$title', metadata =  '$metadata', pagesidenav = '$pageSideNav' , masthead='$pageMasthead'".(($parentid!='') ? ",parentid = $parentid":""). ",updatedby='$createdby' , updatedon = NOW() WHERE  id =".$_REQUEST['id']);

			//$this->createLog('PAGES','Updated page '.$_REQUEST['id'] , $createdby);

			$this->createLog('PAGES' , 'UPDATED' ,  'Updated page '.$_REQUEST['id']);


		}else{
			$validation =  $this->db->query("select * from pages where name = '$name' and valid = 1");

			if($validation->num_rows() > 0 ){
				echo 'INVALID PAGE NAME';
					
				$this->admin(null,null);
			}

			$KWERY1 = "INSERT INTO  pages (id ,name ,title, metadata, content , pagesidenav , masthead ,createdby, updatedby, createdon , updatedon valid ";

			$KWERY2 = ") VALUES (NULL ,  '$name', '$title', '$metadata','$content','$pageSideNav','$pageMasthead', $createdby, $createdby , NOW() , NOW(), true   ";

			IF(($parentid!='')){
				$KWERY1.=", parentid";
				$KWERY2.=",'$parentid'";
			}
			$KWERY2.=")";
			//echo "hhhhhhhhh:::".$KWERY1.$KWERY2;
			$this->db->query($KWERY1.$KWERY2);

			$this->createLog('PAGES' , 'CREATED' ,  "Created New Page having a name of $name");


		}
			
		$this->index();
	}



	public function delete($id){

		$data['deleteId'] = $id;
			
		//$query = $this->db->query("delete from pages where id = $id limit 1" );
			
		$query = $this->db->query("update pages set valid = 0 where id = $id limit 1" );
			
		$this->index();
			
		$this->createLog('PAGES','DELETED',"Deleted a page having an  id of $id");
			
	}
}
